home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / python-usb / examples / usbprint.py < prev   
Encoding:
Python Source  |  2005-10-08  |  3.1 KB  |  126 lines

  1. #!/usr/bin/env python
  2.  
  3. """
  4. Provides an interface to USB Printer Class devices.
  5. """
  6.  
  7. import usb
  8. import types
  9.  
  10. PRINTER_CLASS            = 0x07
  11. PRINTER_SUBCLASS        = 0x01
  12. UNIDIRECTIONAL_PROTOCOL    = 0x01
  13. BIDIRECTIONAL_PROTOCOL    = 0x02
  14. IEEE1284_4_PROTOCOL        = 0x03
  15. VENDOR_PROTOCOL            = 0xff
  16.  
  17. class Printer:
  18.     def __init__(self, device, configuration, interface):
  19.         """
  20.         __init__(device, configuration, interface) -> None
  21.  
  22.         Initialize the device.
  23.             device: printer usb.Device object.
  24.             configuration: printer usb.Configuration object of the Device or configuration number.
  25.             interface: printer usb.Interface object representing the
  26.                        interface and altenate setting.
  27.             
  28.         """
  29.         if PRINTER_CLASS != interface.interfaceClass:
  30.             raise TypeError, "Wrong interface class"
  31.  
  32.         self.__devhandle = device.open()
  33.         self.__devhandle.setConfiguration(configuration)
  34.         self.__devhandle.claimInterface(interface)
  35.         self.__devhandle.setAltInterface(interface)
  36.  
  37.         self.__intf = interface.interfaceNumber
  38.         self.__alt    = interface.alternateSetting
  39.  
  40.         self.__conf = (type(configuration) == types.IntType \
  41.                         or type(configuration) == types.LongType) and \
  42.                         configuration or \
  43.                         configuration.value
  44.  
  45.         # initialize members
  46.         # TODO: automatic endpoints detection
  47.         self.__bulkout    = 1
  48.         self.__bulkin    = 0x82
  49.     
  50.     def __del__(self):
  51.         try:
  52.             self.__devhandle.releaseInterface(self.__intf)
  53.             del self.__devhandle
  54.         except:
  55.             pass
  56.  
  57.     def getDeviceID(self, maxlen, timeout = 100):
  58.         """
  59.         getDeviceID(maxlen, timeout = 100) -> device_id
  60.  
  61.         Get the device capabilities information.
  62.             maxlen: maxlength of the buffer.
  63.             timeout: operation timeout.
  64.         """
  65.         return self.__devhandle.controlMsg(requestType = 0xa1,
  66.                                            request = 0,
  67.                                            value = self.__conf - 1,
  68.                                            index = self.__alt + (self.__intf << 8),
  69.                                            buffer = maxlen,
  70.                                            timeout = timeout)
  71.     
  72.     def getPortStatus(self, timeout = 100):
  73.         """
  74.         getPortStatus(timeout = 100) -> status
  75.  
  76.         Get the port status.
  77.             timeout: operation timeout.
  78.         """
  79.         return self.__devhandle.controlMsg(requestType = 0xa1,
  80.                                            request = 1,
  81.                                            value = 0,
  82.                                            index = self.__intf,
  83.                                            buffer = 1,
  84.                                            timeout = timeout)[0]
  85.  
  86.     def softReset(self, timeout = 100):
  87.         """
  88.         softReset(timeout = 100) -> None
  89.  
  90.         Request flushes all buffers and resets the Bulk OUT
  91.         and Bulk IN pipes to their default states.
  92.             timeout: the operation timeout.
  93.         """
  94.         self.__devhandle.controlMsg(requestType = 0x21,
  95.                                        request = 2,
  96.                                        value = 0,
  97.                                        index = self.__intf,
  98.                                        buffer = 0)
  99.  
  100.     
  101.     def write(self, buffer, timeout = 100):
  102.         """
  103.         write(buffer, timeout = 100) -> written
  104.  
  105.         Write data to printer.
  106.             buffer: data buffer.
  107.             timeout: operation timeout.
  108.         """
  109.         return self.__devhandle.bulkWrite(self.__bulkout,
  110.                                           buffer,
  111.                                           timeout)
  112.  
  113.     def read(self, numbytes, timeout = 100):
  114.         """
  115.         read(numbytes, timeout = 100) -> data
  116.  
  117.         Read data from printer.
  118.             numbytes: number of bytes to read.
  119.             timeout: operation timeout.
  120.         """
  121.         return self.__devhandle.bulkRead(self.__bulkin,
  122.                                          numbytes,
  123.                                          timeout)
  124.  
  125.  
  126.